package com.lyl.xxx.modules.system.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.lyl.common.exception.BizException;
import com.lyl.common.result.BaseCode;
import com.lyl.xxx.modules.framework.security.utils.SecurityUtil;
import com.lyl.xxx.modules.system.account.dao.SysUserDao;
import com.lyl.xxx.modules.system.account.entity.SysUser;
import com.lyl.xxx.modules.system.account.service.SysUserRoleService;
import com.lyl.xxx.modules.system.account.service.SysUserService;
import com.lyl.xxx.modules.web.account.model.dto.ModifyPwdDTO;
import com.lyl.xxx.modules.web.account.model.dto.UpdateUserDTO;
import com.lyl.xxx.modules.web.account.model.dto.UserDTO;
import com.lyl.xxx.modules.web.montior.model.vo.OnlineUserVO;
import com.lyl.xxx.modules.web.account.model.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author Mr Luo
 * @since 2022-09-19 11:14:04
 */
@Service("sysUserService")
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {
    private final SysUserRoleService sysUserRoleService;

    @Override
    public SysUser getByUsername(String username) {
        return lambdaQuery().eq(CharSequenceUtil.isNotBlank(username), SysUser::getUserName, username).one();
    }

    @Override
    public PageInfo<UserVO> list(Integer pageNo, Integer pageSize, UserDTO dto) {
        PageMethod.startPage(pageNo, pageSize);
        List<SysUser> userList = lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(dto.getUserName()), SysUser::getUserName, dto.getUserName())
                .like(CharSequenceUtil.isNotBlank(dto.getPhone()), SysUser::getPhone, dto.getPhone())
                .eq(ObjectUtil.isNotEmpty(dto.getStatus()), SysUser::getStatus, dto.getStatus())
                .ge(CharSequenceUtil.isNotBlank(dto.getStartTime()), SysUser::getCreateTime, dto.getStartTime())
                .le(CharSequenceUtil.isNotBlank(dto.getEndTime()), SysUser::getCreateTime, dto.getEndTime())
                .list();
        PageInfo<SysUser> pageInfo = PageInfo.of(userList);
        // 类型转换
        List<UserVO> voList = userList.stream()
                .map(sysUser -> BeanUtil.copyProperties(sysUser, UserVO.class))
                .collect(Collectors.toList());
        PageInfo<UserVO> voPageInfo = PageInfo.of(voList);
        BeanUtil.copyProperties(pageInfo, voPageInfo, "list");
        return voPageInfo;
    }

    @Override
    public boolean updateUser(UpdateUserDTO dto) {
        SysUser sysUser = BeanUtil.copyProperties(dto, SysUser.class);
        return saveOrUpdate(sysUser);
    }

    @Override
    public boolean modifyPwd(ModifyPwdDTO dto) {
        //修改密码前置条件：只有管理员和自己能修改密码
        //1. 判断当前用户的身份
        long loginUserId = SecurityUtil.getUserId();
        List<Long> adminIds = sysUserRoleService.getAdminUserIds();
        Assert.isTrue(adminIds.contains(loginUserId) || ObjectUtil.equals(loginUserId,dto.getUserId()), () -> new BizException(BaseCode.ACCESS_DENIED,"权限不足，无法修改用户密码"));
        //2. 修改密码
        return updateById(SysUser.builder()
                .userId(dto.getUserId())
                .password(BCrypt.hashpw(dto.getPwd()))
                .build());
    }

    @Override
    public PageInfo<OnlineUserVO> getOnlineUserList(Integer pageNo, Integer pageSize) {
        return null;
    }
}

